package b.acct.api

import akka.{Done, NotUsed}
import b.utils.formats.JsonFormats
import com.lightbend.lagom.scaladsl.api.{Descriptor, Service, ServiceCall}
import play.api.libs.json.{Format, Json}

/**
  * 贲国称 于 2016/12/9 创建。
  */

case class CC开户Request(v客户号: String, v产品号: Long)
object CC开户Request {
  implicit val format1: Format[CC开户Request] = Json.format
}
case class CC账户(v账号: String, v余额: BigDecimal = 10.00, v状态: E账户状态定义 = E账户状态.e正常) {
  def f余额变动(v金额: BigDecimal): CC账户 = copy(v余额 = v余额 + v金额)
}
object CC账户 {
  implicit val format1: Format[CC账户] = Json.format
}
case class CC转账Request(v转出账号: String, v转入账号: String, v转账金额: BigDecimal)
object CC转账Request {
  implicit val format1: Format[CC转账Request] = Json.format
}
object E账户状态 extends Enumeration {
  val e正常 = Value("N")
  val e冻结 = Value("F")
  val e销户 = Value("X")
  private val nameOfValues = Seq[String]("正常", "冻结", "销户")
  def nameOf(v: Value): String = nameOfValues(v.id)
  implicit val format: Format[Value] = JsonFormats.enumFormat(E账户状态)
}

trait T账户Service extends Service {

  def f开户(): ServiceCall[CC开户Request, String]
  def f转账(): ServiceCall[CC转账Request, String]
  def f账户查询(v账号: String): ServiceCall[NotUsed, CC账户]

  override def descriptor(): Descriptor = {
    import Service._
    named("ACCT").withCalls(
      namedCall("/acct/accounts", f开户 _),
      pathCall("/acct/transactions", f转账 _),
      pathCall("/acct/accounts/:v账号", f账户查询 _)
    ).withAutoAcl(true)
  }

}
